import os
import xml.dom.minidom
from PIL import Image
read_file = '/home/user/my/img_new/test_xml/'
out_xmlfile = '/usr/VOCdevkit/VOC2019/Annotation/'
JPEGImageDir = '/usr/VOCdevkit/VOC2019/JEPGImages/'

for file_name in os.listdir(read_file):
    new_txtname = file_name.split('.')[0]
    curImg = Image.open(read_file+file_name)


    # 创建一个空的Dom文档对象
    doc = xml.dom.minidom.Document()
    # 创建根节点，此根节点为annotation
    annotation = doc.createElement('annotation')
    # 将根节点添加到DOm文档对象中
    doc.appendChild(annotation)

    folder = doc.createElement('folder')
    # 内容写入
    folder_text = doc.createTextNode('JPEGImages')
    folder.appendChild(folder_text)
    annotation.appendChild(folder)

    filename = doc.createElement('filename')
    filename_text = doc.createTextNode(file_name)
    filename.appendChild(filename_text)
    annotation.appendChild(filename)

    path = doc.createElement('path')
    path_text = doc.createTextNode(JPEGImageDir+file_name)
    path.appendChild(path_text)
    annotation.appendChild(path)

    source = doc.createElement('source')
    databass = doc.createElement('database')
    databass_text = doc.createTextNode('Unknown')
    source.appendChild(databass)
    databass.appendChild(databass_text)
    annotation.appendChild(source)

    size = doc.createElement('size')
    width = doc.createElement('width')

    width_text = doc.createTextNode(str(curImg.size[0]))
    height = doc.createElement('height')
    height_text = doc.createTextNode(str(curImg.size[1]))
    depth = doc.createElement('depth')
    depth_text = doc.createTextNode('3')
    size.appendChild(width)
    width.appendChild(width_text)
    size.appendChild(height)
    height.appendChild(height_text)
    size.appendChild(depth)
    depth.appendChild(depth_text)
    annotation.appendChild(size)

    segmented = doc.createElement('segmented')
    segmented_text = doc.createTextNode('0')
    segmented.appendChild(segmented_text)
    annotation.appendChild(segmented)

    object = doc.createElement('object')
    name = doc.createElement('name')
    name_text = doc.createTextNode('ccc')
    name.appendChild(name_text)
    pose = doc.createElement('pose')
    pose_text = doc.createTextNode('Unspecified')
    pose.appendChild(pose_text)
    truncated = doc.createElement('truncated')
    truncated_text = doc.createTextNode('0')
    truncated.appendChild(truncated_text)
    difficult = doc.createElement('difficult')
    difficult_text = doc.createTextNode('0')
    difficult.appendChild(difficult_text)


    bndbox = doc.createElement('bndbox')
    xmin = doc.createElement('xmin')
    ymin = doc.createElement('ymin')
    xmax = doc.createElement('xmax')
    ymax = doc.createElement('ymax')
    xmin_text = doc.createTextNode('123') #biao zhu kuang x
    xmin.appendChild(xmin_text)
    xmax_text = doc.createTextNode('456')
    xmax.appendChild(xmax_text)
    ymin_text = doc.createTextNode('234')
    ymin.appendChild(ymin_text)
    ymax_text = doc.createTextNode('345')
    ymax.appendChild(ymax_text)
    bndbox.appendChild(xmin)
    bndbox.appendChild(xmax)
    bndbox.appendChild(ymin)
    bndbox.appendChild(ymax)
    annotation.appendChild(object)
    object.appendChild(name)
    object.appendChild(pose)
    object.appendChild(truncated)
    object.appendChild(difficult)
    object.appendChild(bndbox)

    # 写入xml文本文件中
    fp = open('/home/user/my/img_new/xml/%s.xml' % new_txtname, 'w')
    # doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8')
    doc.writexml(fp, addindent='\t', newl='\n', encoding='utf-8')
    fp.close()
    os.system('chown -R user /home/user/my/img_new/')